package com.example.demo.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.ConnectionMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Connection;
import com.example.demo.pojo.Score;
import com.example.demo.pojo.User;
import com.example.demo.service.IConnectionService;
import com.example.demo.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class ConnectionServiceImpl extends ServiceImpl<ConnectionMapper,Connection> implements IConnectionService {

    @Autowired
    ConnectionMapper connectionMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Integer> getDurations(int id) {
        List<Connection> list=new ArrayList<>();
        list=connectionMapper.getDurations(id);
        Collections.sort(list,(a,b)-> Tools.getSeconds(b.getEndDate(),a.getEndDate()));
        List<Integer> res=new ArrayList<>();
        int length= Math.min(list.size(), 5);
        for(int i=0;i<length;i++){
            res.add(list.get(i).getDuration());
        }
        return res;
    }

    @Override
    public List<Integer> getScores(int id) {
        List<Score> list=new ArrayList<>();
        list=connectionMapper.getScores(id);
        Collections.sort(list,(a,b)-> Tools.getSeconds(b.getEndDate(),a.getEndDate()));
        List<Integer> res=new ArrayList<>();
        int length= Math.min(list.size(), 5);
        for(int i=0;i<length;i++){
            res.add(list.get(i).getScore());
        }
        return res;
    }
    @Override
    //根据发起或接收用户id获取已连接connection
    public Connection getConnection(int id){
        return connectionMapper.getConnection(id);
    }

    public void insertConn(Connection connection,int sendId,int receiveId){
        connection.setConnection_status("wait");
        String sendUsername = userMapper.getUserById(sendId).getUsername();
        connection.setSend_user_id(sendId);
        connection.setSend_user_name(sendUsername);
        String receiveUsername=userMapper.getUserById(receiveId).getUsername();
        connection.setReceive_user_id(receiveId);
        connection.setReceive_user_name(receiveUsername);
        System.out.println("receiveId"+receiveId);
        connectionMapper.insert(connection);
    }
    //建立连接并修改连接用户状态为on
    public boolean beginConn(int conn_id,int receive_id){
        Connection connection=connectionMapper.selectById(conn_id);
        connection.setReceive_user_id(receive_id);

        User sendUser=userMapper.getUserById(connection.getSend_user_id());
        if(sendUser.getState().equals("off")){
            return false;
        }
        User receiveUser=userMapper.getUserById(receive_id);
        String receive_name=receiveUser.getUsername();
        sendUser.setState("on");
        receiveUser.setState("on");

        connection.setReceive_user_name(receive_name);
        connection.setConnection_status("on");
        connection.setBeginDate(new Timestamp(System.currentTimeMillis()));

        UpdateWrapper<Connection> con_updateWrapper=new UpdateWrapper<>();
        con_updateWrapper.eq("connection_id",conn_id);
        connectionMapper.update(connection,con_updateWrapper);

        UpdateWrapper<User> send_updateWrapper=new UpdateWrapper<>();
        send_updateWrapper.eq("user_id",sendUser.getUser_id());
        userMapper.update(sendUser,send_updateWrapper);

        UpdateWrapper<User> receive_updateWrapper=new UpdateWrapper<>();
        receive_updateWrapper.eq("user_id",receive_id);

        userMapper.update(receiveUser,receive_updateWrapper);
        return true;
    }
    //断开连接
    public void endConn(int id){
        Connection connection=getConnection(id);
        User sendUser=userMapper.getUserById(connection.getSend_user_id());
        User receiveUser=userMapper.getUserById(connection.getReceive_user_id());



        connection.setEndDate(new Timestamp(System.currentTimeMillis()));
        connection.setDuration(Tools.getDays(connection.getBeginDate(),new Timestamp(System.currentTimeMillis())));
        connection.setConnection_status("off");

        sendUser.setState("lock");
        receiveUser.setState("lock");
        Timestamp next=new Timestamp(System.currentTimeMillis()+86400000);
        sendUser.setLastTime(next);
        receiveUser.setLastTime(next);

        UpdateWrapper<Connection> con_updateWrapper=new UpdateWrapper<>();
        con_updateWrapper.eq("connection_id",id);


        UpdateWrapper<User> send_updateWrapper=new UpdateWrapper<>();
        send_updateWrapper.eq("user_id",sendUser.getUser_id());


        UpdateWrapper<User> receive_updateWrapper=new UpdateWrapper<>();
        receive_updateWrapper.eq("user_id",receiveUser.getUser_id());


        connectionMapper.update(connection,con_updateWrapper);
        userMapper.update(sendUser,send_updateWrapper);
        userMapper.update(receiveUser,receive_updateWrapper);
    }

    public List<Connection> getConByReceiveId(int id){
        QueryWrapper<Connection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("receive_user_id",id);
        queryWrapper.eq("connection_status","wait");
        return connectionMapper.selectList(queryWrapper);
    }

    public void removeCon(int con_id){
        UpdateWrapper<Connection> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("connection_id",con_id).set("connection_status","no");
        connectionMapper.update(null,updateWrapper);
    }
    public List<Connection> getAllConnections(){
        return connectionMapper.selectList(null);
    }
}
